﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DeliveryWebSite;
using DeliveryWebSite.Models;

namespace DeliveryWebSite.Controllers
{
    public class OrderController : Controller
    {

        //
        // GET: /Order/

        public ActionResult Index()
        {
            ViewBag.Message = "Hello Orders.";
            return View();
        }

        public ActionResult NewOrders()
        {
            using (DeliveryDbEntities db = new DeliveryDbEntities())
            {
                var query = db.OrderForm.Include("Address").Include("Students")
                            .Where(o => o.Status == (int)OrderStatus.waitingShipped
                                    /* && System.Data.Objects.EntityFunctions.DiffDays(o.CreateTime, DateTime.Now) == 0*/)
                              .OrderByDescending(o => o.CreateTime).ToList();

                return View(query);
            }
        }

        public ActionResult UpdateOrderStatus(string orderNumber, int status)
        {
            using (DeliveryDbEntities db = new DeliveryDbEntities())
            {
                var order = db.OrderForm.Single(o => o.OrderFormNumber == orderNumber);
                order.Status = status;

                var history = new OrderForm_History();
                history.OrderForm = order;
                history.OrderForm_Maker = db.OrderForm_Maker.FirstOrDefault();
                history.OrderStatus = status;
                history.SubmitTime = DateTime.Now;
                db.OrderForm_History.Add(history);
                db.SaveChanges();
                return RedirectToAction("NewOrders");
            }
        }

        //
        // GET: /Order/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Order/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Order/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Order/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Order/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Order/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Order/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


    }
    public enum OrderStatus
    {
        /// <summary>
        /// 待发货
        /// </summary>
        waitingShipped = 0,
        /// <summary>
        /// 待收货/已发货
        /// </summary>
        waitingReceipt,
        /// <summary>
        /// 已完成/已收货
        /// </summary>
        Finished,
        /// <summary>
        /// 已取消
        /// </summary>
        Cancelled
    }
}
